var formToSubmit = "#general_form";
var formbutton = "#submit_general";

$(document).ready(function() {

        function init_new_object()
        {
            $('#object_type_ID').live('change',function(){
                    if($(this).val()==0)return;

                    var url = base_url+'objects/general_html/'+$(this).val()
                    $.ajax({
                            url: url,
                            dataType: 'json',
                            type: "POST",
                            success: function (data, textStatus, xhr) {

                                $('.data-container').html(data.general_html);
                                
                                $('.view_add_attributes_on_group').attr('data-holder',data.general_group_id)
                                $('.view_attributes_on_group').attr('data-holder',data.general_group_id)

                                $(formbutton).removeAttr("disabled");

                            },
                            error: function (xhr, textStatus, errorThrown) {
                                alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                            }
                    });  

            });



        }

        function appent_tabs_links(insert_id)
        {
            var elems = $('.nav-tabs .hidden');
            //log(elems)
            $.each(elems, function(index, value) { 
                    //log($(value).find('a').attr('href'));
                    var link = $(value).find('a').attr('href');
                    $(value).find('a').attr('href',link+'/'+insert_id+'?post_type=realestate');
                    //log($(value).find('a').attr('href'));
                    $(value).removeClass('hidden');
            });
        }

        switch (post_action) {
            case "EDIT":
                myObject['mode'] = "EDIT";
                myObject['id'] = object_ID;
                $(formbutton).removeAttr("disabled");
                break;
            case "NEW":
                myObject['mode'] = "NEW";
                init_new_object();
                $('#object_type_ID').val(0);
                $('#object_destination').val(0);
                break;
        }  

        submit_success = function (data, textStatus, xhr)
        {

            if(data.action==true){

                $(formToSubmit+' .status_info').html(alert_html(data.msg,'success')); 

                function action_delay(){              
                    //append links in tabs navigation
                    appent_tabs_links(data.insert_id);
                }
                window.setTimeout(function() { action_delay(); }, 1001);

            }else{

                $(formbutton).removeAttr("disabled");
                $(formToSubmit+' .status_info').html(alert_html(data.msg,'error'));
                $(formToSubmit).animate({
                        scrollTop:  $(formToSubmit+' .status_info').offset().top
                    }, 200);
            }
        }


});